﻿using System;
using System.Web.UI.WebControls;

namespace VioletDevelopmentLaboratory.Net.Web.Component
{
	public abstract class BoardFormPartBase : BoardTemplateBase
	{
		public virtual void AccessValidate()
		{
			Board CurrentBoard = (Board)this.Page.Items["CurrentBoard"];
			VBoardUserInfo CurrentUserInfo = (VBoardUserInfo)this.Page.Items["CurrentUserInfo"];
			BoardThread CurrentThread = (BoardThread)this.Page.Items["CurrentThread"];

			String returnUrl = BoardUtil.GetBoardUrl(BoardPart.List, BoardParameters.PageIndex, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0);

			if (Page.Request.UrlReferrer == null)
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("General_InvalidAccess"), returnUrl, true, 3);

			if (BoardParameters.Part == BoardPart.Form_Add)
			{
				//글등록 개수 제한이 있는지 검사
				if (CurrentBoard.ThreadLimit != -1 && (CurrentBoard.ThreadLimit <= CurrentBoard.GetThreadCollectionCount("", "")))
					VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadWrite_Limit"), returnUrl, true, 3);

				//글쓰기 권한이 있는지 검사
				if (!(CurrentUserInfo.IsAdministrator ||
					CurrentUserInfo.IsSysop(CurrentBoard) ||
					CurrentUserInfo.IsAuthorizedBy(CurrentBoard, BoardCommand.ThreadWrite)))
					VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadWrite_Rejection"), returnUrl, true, 3);
			}
			else if (BoardParameters.Part == BoardPart.Form_Modify)
			{
				//시샵 또는 글작성자인지 확인
				if (!(CurrentUserInfo.IsAdministrator ||
					CurrentUserInfo.IsSysop(CurrentBoard) ||
					(CurrentUserInfo.ProviderUserKey == CurrentThread.ProviderUserKey)))
					VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadUpdate_Rejection"), returnUrl, true, 3);
			}
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			AccessValidate();
		}
	}
}
